require 'openssl'
require 'digest'
require 'jsonify'
module CompanyService
  URL = "http://182.148.48.165:8090/open.api"
  Version = "1.0"
  Format = "json"
  def self.company_upload(company_id, project_id, type)
    conn = Faraday.new(:url => URL)
    method = "Corp.Upload"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    project = Project.find(project_id)
    company = Company.find(company_id)
    compnay_type = CompanyType.find(company.register_type)
    company_data = Jsonify::Builder.new
    company_data.corpCode  company.credit_code
    company_data.corpName  company.company_name
    company_data.corpType  compnay_type.type_code.to_s
    company_data.areaCode  "370202"
    company_data.registerDate  WorkManageService.get_timestamp_other(company.register_time)
    data = company_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    
    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      yibu = YiBu.new
      yibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      yibu.m_type = 1
      yibu.m_id = company.id
      yibu.save
      company.approvl_status = 3
      company.save
      return 0
    else
      return 400
    end
  end
  def self.sub_contractor_upload(project_id, type)
    conn = Faraday.new(:url => URL)
    method = "ProjectSubContractor.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    project = Project.find(project_id)
    company_data = Jsonify::Builder.new
    company_data.projectCode project.app_id
    company_data.corpCode  project.construction_code
    company_data.corpName  project.construction_name
    company_data.corpType  "002"
    data = company_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    
    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      yibu = YiBu.new
      yibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      yibu.m_type = 1
      yibu.m_id = -999
      yibu.save
      # company.approvl_status = 3
      # company.save
      return 0
    else
      return 400
    end
  end
	def self.jiaoyan_company_upload(company_id, project_id)
		yibu = YiBu.where("m_id = ? and m_type = ? and chuli_status = 3", company_id, project_id)
		if yibu.nil?
			yibu = YiBu.where("m_id = ? and m_type = ? and chuli_status in(1,2) ", company_id, project_id)
			if yibu.nil?
				return -1 ## 没有成功上传企业	 
			else 
				return yibu.id   # 企业正在上传，可以在该步骤执行完再进行下一步
			end
		else
			return 0  ## 企业已经成功上传，可以执行下一步
		end
	end
 end